package top.chmin.project.cloud.builder.config.info.auto;

import org.springframework.stereotype.Repository;
import top.chmin.project.cloud.common.dao.BaseDao;
import top.chmin.project.cloud.common.entity.Condition;
import top.chmin.project.cloud.common.entity.Page;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * chmin
 * 2023-12-25 09:05:44
 * cloud-builder
 */
@Repository
public class ConfigInfoDao extends BaseDao<ConfigInfo> {

    @Override
    public int save(ConfigInfo model) {
        StringBuilder sb = new StringBuilder("INSERT INTO config_info(");
// field column auto
// #bind.field.javaPath#
        sb.append("java_path,");
// $bind.field.javaPath$

// #bind.field.uiPath#
        sb.append("ui_path,");
// $bind.field.uiPath$

// #bind.field.configName#
        sb.append("config_name,");
// $bind.field.configName$

// #bind.field.configCode#
        sb.append("config_code,");
// $bind.field.configCode$

        sb.append("create_by,update_by,create_time,update_time");
        sb.append(") VALUES (");
// field ? auto
// #bind.field.javaPath#
        sb.append("?,"); // java_path
// $bind.field.javaPath$

// #bind.field.uiPath#
        sb.append("?,"); // ui_path
// $bind.field.uiPath$

// #bind.field.configName#
        sb.append("?,"); // config_name
// $bind.field.configName$

// #bind.field.configCode#
        sb.append("?,"); // config_code
// $bind.field.configCode$

        sb.append("?,?,?,?");
        sb.append(")");

        return template.update(sb.toString(),
// field value auto
// #bind.field.javaPath#
                model.getJavaPath(),
// $bind.field.javaPath$

// #bind.field.uiPath#
                model.getUiPath(),
// $bind.field.uiPath$

// #bind.field.configName#
                model.getConfigName(),
// $bind.field.configName$

// #bind.field.configCode#
                model.getConfigCode(),
// $bind.field.configCode$

                model.getCreateBy(),
                model.getUpdateBy(),
                model.getCreateTime(),
                model.getUpdateTime()
        );
    }

    @Override
    public int save(List<ConfigInfo> list) {
        StringBuilder sb = new StringBuilder("INSERT INTO config_info(");
// field column auto
// #bind.field.javaPath#
        sb.append("java_path,");
// $bind.field.javaPath$

// #bind.field.uiPath#
        sb.append("ui_path,");
// $bind.field.uiPath$

// #bind.field.configName#
        sb.append("config_name,");
// $bind.field.configName$

// #bind.field.configCode#
        sb.append("config_code,");
// $bind.field.configCode$

        sb.append("create_by,update_by,create_time,update_time");
        sb.append(") VALUES (");
// field ? auto
// #bind.field.javaPath#
        sb.append("?,"); // java_path
// $bind.field.javaPath$

// #bind.field.uiPath#
        sb.append("?,"); // ui_path
// $bind.field.uiPath$

// #bind.field.configName#
        sb.append("?,"); // config_name
// $bind.field.configName$

// #bind.field.configCode#
        sb.append("?,"); // config_code
// $bind.field.configCode$

        sb.append("?,?,?,?");
        sb.append(")");
        return Arrays.stream(template.batchUpdate(sb.toString(),
                list.stream()
                        .map(model -> new Object[]{
// field value auto
// #bind.field.javaPath#
                                model.getJavaPath(),
// $bind.field.javaPath$

// #bind.field.uiPath#
                                model.getUiPath(),
// $bind.field.uiPath$

// #bind.field.configName#
                                model.getConfigName(),
// $bind.field.configName$

// #bind.field.configCode#
                                model.getConfigCode(),
// $bind.field.configCode$

                                model.getCreateBy(),
                                model.getUpdateBy(),
                                model.getCreateTime(),
                                model.getUpdateTime()
                        })
                        .toList()
        )).sum();
    }

    @Override
    public int updateById(ConfigInfo model) {
        return updateByCondition(model.getPreCondition().forUpdate().id().eq().toCondition());
    }

    @Override
    public int deleteById(Long id) {
        return deleteByCondition(new ConfigInfoFPC().forDelete().id().eq(id).toCondition());
    }

    @Override
    public int deleteById(List<Long> list) {
        return deleteByCondition(new ConfigInfoFPC().forDelete().id().in(list).toCondition());
    }

    @Override
    public ConfigInfo getById(Long id) {
        return getStreamByCondition(new ConfigInfoFPC().id().eq(id).toCondition(), ConfigInfo.class).findFirst().orElse(null);
    }

    @Override
    public List<ConfigInfo> getListById(List<Long> list) {
        return getStreamByCondition(new ConfigInfoFPC().id().in(list).toCondition(), ConfigInfo.class).toList();
    }

    @Override
    public ConfigInfo getFirst(Condition condition) {
        return getFirst(condition, ConfigInfo.class);
    }

    @Override
    public Stream<ConfigInfo> getStreamByCondition(Condition condition) {
        return getStreamByCondition(condition, ConfigInfo.class);
    }

    @Override
    public Page<ConfigInfo> getPageByCondition(Condition condition) {
        return getPageByCondition(condition, ConfigInfo.class);
    }
}
